package com.clearliang.jpushframe;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private TextView tvText;
    private Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        localBroadcastManager = LocalBroadcastManager.getInstance(this);

        intentFilter = new IntentFilter();
        intentFilter.addAction("com.clearliang.AnotherBroadcastReceiver");
        localReceiver = new LocalReceiver();
        //注册本地接收器
        localBroadcastManager.registerReceiver(localReceiver, intentFilter);
    }

    private void initView() {
        tvText = (TextView) findViewById(R.id.tv_text);
        btn1 = (Button) findViewById(R.id.btn_1);
    }

    private IntentFilter intentFilter;
    private LocalReceiver localReceiver;
    private LocalBroadcastManager localBroadcastManager;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(localReceiver);
    }

    private class LocalReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            String title = intent.getExtras().getString("title");
            String message = intent.getExtras().getString("message");
            Toast.makeText(context, "收到本地广播" + title + message, Toast.LENGTH_SHORT).show();
            initRemoteViews(title, message);
        }
    }

    private void initRemoteViews(String title, String message) {
        //1.创建RemoteViews实例
        //RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget_remote);
        //remoteViews.setTextViewText(R.id.tv_widget_remote_1, "123456qwerty");
        //remoteViews.setImageViewResource(R.id.imageView3, R.drawable.qmui_icon_checkbox_checked);

        //2.构建一个打开Activity的PendingIntent
        //Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        //PendingIntent mPendingIntent = PendingIntent.getActivity(LoginActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        //3.创建一个Notification
        //showNotification(int largeBitmap,String title,String text,int smallIcon,Class intentClass)
        //NotificationUtil.getNotificationUtils(LoginActivity.this).showNotification(null, mPendingIntent);
        NotificationUtil.getNotificationUtils(MainActivity.this).showNotification(
                R.drawable.ic_launcher_background,
                title, message, R.drawable.ic_launcher_foreground, MainActivity.class);

    }
}
